package com.khogame.vstore;

import android.content.Context;
import android.os.AsyncTask;
import android.provider.Settings.Secure;
import android.util.Log;
import android.vstore.util.GetUrlMethod;

import com.google.android.gcm.GCMRegistrar;

public class GCMRegister {

	public static String TAG = "TAG";
	public static String urlRegister = "http://123.30.129.248/api/registerPushNotification.php";
	public static String mRegId = "";
	public static void Register(final Context context)
	{
		final String android_id = Secure.getString(
				context.getContentResolver(),
				Secure.ANDROID_ID);
		
		new AsyncTask<Void, String , String>(){
			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
			}

			@Override
			protected String doInBackground(Void... params) {
				// TODO Auto-generated method stub
				// Make sure the device has the proper dependencies.
				GCMRegistrar.checkDevice(context);

				// Make sure the manifest was properly set - comment out this line
				// while developing the app, then uncomment it when it's ready.
				GCMRegistrar.checkManifest(context);

				// Get GCM registration id
				mRegId = GCMRegistrar.getRegistrationId(context);
				// Check if regid already presents
				if (mRegId.equals("")) {

					GCMRegistrar.register(context, GCMIntentService.SENDER_ID);
				}
				Log.d("TAG", "mRegId: " + mRegId);
				return null;
				
			}
			
			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						GetUrlMethod.postRequest(urlRegister +"?deviceToken="+mRegId +"&deviceID="+android_id);
					}
				}).start();
			}
			
		}.execute();
	}
	
	
	
}
